package prototype;

public class ForwardBehaviour implements Runnable {

	private Agent agent;
	private static boolean released;

	public ForwardBehaviour(Agent a) {
		agent = a;
		released = false;
	}

	@Override
	public void run() {
		while (true) {
			synchronized (agent) {
				if (agent.moving()) {
					agent.notifyAll();
				} else {
					while (!released)
						try {
							agent.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					if (!agent.moving()) {
						agent.forward();
					}
					agent.notifyAll();
				}
			}
		}
	}

	public static void release() {
		released = true;
	}

	public static void unrelease() {
		released = false;
	}

}
